package com.gskinner.text
{
    import flash.text.*;

    public class TextFlow extends Object
    {
        private var _textFields:Array;
        public var orphans:uint = 2;
        private var _text:String;
        public var widows:uint = 2;
        private var textObjects:Array;

        public function TextFlow(param1:Array, param2:String = null)
        {
            orphans = 2;
            widows = 2;
            _text = param2 == null ? (param1[0].text) : (param2);
            this.textFields = param1;
            return;
        }

        public function get textFields() : Array
        {
            return _textFields;
        }

        public function reflow() : void
        {
            var l:uint;
            var lastIndex:Number;
            var endOfText:Boolean;
            var i:uint;
            var fld:TextField;
            var obj:Object;
            var nextLineIndex:uint;
            var offset:uint;
            var pstart:uint;
            var pend:uint;
            l = _textFields.length;
            lastIndex;
            endOfText;
            i;
            while (i < l)
            {
                
                fld = _textFields[i];
                obj = textObjects[i];
                fld.text = ltrim(_text.substr(lastIndex));
                fld.scrollV = 1;
                nextLineIndex = fld.bottomScrollV;
                try
                {
                    offset = fld.getLineOffset(nextLineIndex);
                }
                catch (e)
                {
                    var _loc_3:* = _text.length;
                    lastIndex = _text.length;
                    obj.end = _loc_3;
                    fld.text = rtrim(fld.text.substr(0));
                    ;
                }
                if (orphans > 1 && widows > 1)
                {
                    pstart = fld.getLineIndexOfChar(fld.getFirstCharInParagraph(offset));
                    pend = fld.getLineIndexOfChar(fld.getFirstCharInParagraph(offset) + fld.getParagraphLength(offset));
                    if (pend > nextLineIndex && pstart < nextLineIndex)
                    {
                        if (nextLineIndex - pstart < orphans)
                        {
                            offset = fld.getFirstCharInParagraph(offset);
                        }
                        else if (pend - nextLineIndex < widows)
                        {
                            offset = fld.getLineOffset(pend - widows);
                        }
                    }
                }
                fld.text = rtrim(fld.text.substr(0, offset));
                lastIndex = lastIndex + offset;
                obj.end = lastIndex;
                i = (i + 1);
            }
            return;
        }

        public function get text() : String
        {
            return _text;
        }

        public function getOverflow(param1:int = -1, param2:Boolean = true) : String
        {
            var _loc_3:String = null;
            if (param1 < 0)
            {
                param1 = _textFields.length + param1 % _textFields.length;
            }
            _loc_3 = _text.substr(textObjects[param1].end);
            return param2 ? (ltrim(_loc_3)) : (_loc_3);
        }

        private function rtrim(param1:String) : String
        {
            return param1.replace(/\s+$/, "");
        }

        private function ltrim(param1:String) : String
        {
            return param1.replace(/^\s*[\n\r]/, "");
        }

        public function set text(param1:String) : void
        {
            _text = param1;
            reflow();
            return;
        }

        public function set textFields(param1:Array) : void
        {
            var _loc_2:uint = 0;
            _textFields = param1 == null ? ([]) : (param1);
            textObjects = [];
            _loc_2 = 0;
            while (_loc_2 < param1.length)
            {
                
                textObjects[_loc_2] = {};
                _loc_2 = _loc_2 + 1;
            }
            reflow();
            return;
        }

    }
}
